#include "stdafx.h"
#include "Keylistner.h"
#include "AddNodeDialog.h"
#include "iostream"
#include "SaveTo.h"
#include "ReadFrom.h"
#include "Paste.h"
#include "GetFile.h"




using namespace RedDot;
Keylistner::Keylistner(System::Windows::Forms::KeyEventArgs^ e)
{
	//E=e->KeyData;
	tField=false;
}

Keylistner::Keylistner(Root *tr)
{
	tree=tr;
	wezel=(Node*)tr;
	shift=false;
	control=false;
	tField=false;
}
//Bury 10.04.07 od
//funkcja edytujaca nody - info w Keylistner.h
void Keylistner::editNode(Node* wezel)
{
	AddNodeDialog ^dialog2 = gcnew AddNodeDialog(2,tree,wezel,wezel->getNodeName());//gdy pierwszym argumentem jest "2" to edycja
	dialog2->ShowDialog();

}
//Bury 10.04.07 do

void Keylistner::KeyDown(System::Windows::Forms::KeyEventArgs^ e)
{
	//System::Windows::Forms::Keys E;
	E=e->KeyCode;
		Node *n1 = new Node("",0);
	if(!e->Control&&!e->Alt&&!e->Shift)
	switch(E){
		case (Windows::Forms::Keys::Insert):
		
			wezel->addChild("",n1);
			
			tField=true;
			tF_X = wezel->last->getX();
			tF_Y = wezel->last->getY();
			edited = wezel->last;

			break;
			
			
			
			
			
			
			
			/*{
				AddNodeDialog ^ dialog = gcnew AddNodeDialog(0,tree,wezel);
//				dialog->currentWorksheet = this->currentWorksheet;
				dialog->ShowDialog();
//				pozycja=currentWorksheet->getCount()-1;	
				wezel=wezel->last;
			}
			break;*/
		case (Windows::Forms::Keys::V):
			{
				if(wezel->first!=NULL)	
				  wezel->visibility();
			}
			break;
		case (Windows::Forms::Keys::R):
			{
				if(wezel->link==true){
				GetFile(wezel->NodeName.c_str());
				Node *ble=new Node("nazwa",0);
				if(wezel->first)
					wezel->first->remove();
				ble=ReadFrom("C:/Documents and Settings/RedDot/net.rdt",NULL);
				ble->setInvisible();
				Paste(ble,wezel);
				ble->setVisible();
				wezel=ble;
				wezel->link=false;
				if(wezel->previous) wezel->previous->focus=false;
				wezel->focus=1;
				}
			}
			break;
		//Bury 10.04.07 od
		case (Windows::Forms::Keys::F2)://edycja nodow za pomoca klawiatury
			{
					editNode(wezel);
			}
			break;
        //Bury 10.04.07 do

		case (Windows::Forms::Keys::Enter):
			{  
            
			
			
			
			if(wezel->father) {
			   wezel->addBrother("",n1);
			   tF_X = wezel->next->getX();
			   tF_Y = wezel->next->getY();
			   edited=wezel->next;
			   tField=true;
			}	else {
				/*
				tF_X = wezel->last->getX();
			   tF_Y = wezel->last->getY();
			   edited=wezel->last;*/
				this->editNode(wezel);
			}	
				
			
				break;
			}	
		case (Windows::Forms::Keys::Up):
				wezel=wezel->inUp();
			
			break;
		case (Windows::Forms::Keys::Down):
				wezel=wezel->inDown();
			break;
		case (Windows::Forms::Keys::Right):
				wezel=wezel->inRight();
			break;
		case (Windows::Forms::Keys::Left):
				wezel=wezel->inLeft();
			break;
//Pawel 13.03.07 od
		case (Windows::Forms::Keys::Delete):
				Node *tmp=wezel;
				if(wezel->father)
				{
					wezel=wezel->remove();
					delete tmp;
				}

			break;
//Pawel 13.03.07 do

	}
	if(!e->Control&&e->Alt&&!e->Shift)
	{
		switch(E)
		{
			case (Windows::Forms::Keys::Up):
				wezel->move(0,3);
				break;
			case (Windows::Forms::Keys::Down):
				wezel->move(0,-3);
				break;
			case (Windows::Forms::Keys::Right):
				wezel->move(3,0);
				break;
			case (Windows::Forms::Keys::Left):
				wezel->move(-3,0);
				break;
		}
	}

	if(e->Control&&!e->Alt&&!e->Shift)
		switch(E){
			case (Windows::Forms::Keys::C):
					SaveTo("C:/Documents and Settings/RedDot/tmp.rdt",wezel,NULL);
				break;
			case (Windows::Forms::Keys::V):
				{
					Node *ble=new Node("nazwa",0);
					ble=ReadFrom("C:/Documents and Settings/RedDot/tmp.rdt",NULL);
					ble->setInvisible();
					Paste(ble,wezel);
					ble->setVisible();
					wezel=ble;
					if(wezel->previous) wezel->previous->focus=false;
					wezel->focus=1;
				break;
				}
			case (Windows::Forms::Keys::Up):
					wezel->nodeUp();
				break;
			case (Windows::Forms::Keys::Down):
					wezel->nodeDown();
				break;
			case (Windows::Forms::Keys::Left):
					wezel->nodeLeft();
				break;
			case (Windows::Forms::Keys::Enter):
			{  

					AddNodeDialog ^dialog1 = gcnew AddNodeDialog(1,tree,wezel);
					dialog1->ShowDialog();
					if(wezel->father) wezel=wezel->next;
					else wezel=wezel->last;
				
			break;
			}	
			case (Windows::Forms::Keys::Insert): 
             {
				AddNodeDialog ^ dialog = gcnew AddNodeDialog(0,tree,wezel);
//				dialog->currentWorksheet = this->currentWorksheet;
				dialog->ShowDialog();
//				pozycja=currentWorksheet->getCount()-1;	
				wezel=wezel->last;
			    break;
			}
	}

}

